﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Linq.Expressions;

namespace JingQiao.Ads.Infrastructure.Data.EF.Repository
{
    using Domain.Model;
    using Domain.Model.DomainObject;
    using Domain.Model.Repository;

    public class PositionRepository : BaseRepository<IPosition>, IPositionRepository
    {
        public PositionRepository(IContext context)
            : base(context)
        {
        }

        public override void Add(IPosition entity)
        {
            Check.Argument.IsNotNull(entity, "entity");

            if (this._context.Position.Any(p => p.UniqueCode == entity.UniqueCode))
            {
                throw new ArgumentException("\"{0}\" Position with the same UniqueCode already exits. Specifiy a diffrent UniqueCode.".FormatWith(entity.UniqueCode), "entity");
            }

            base.Add(entity);
        }

        public bool IsHasAdvertisement(int positionId)
        {
            return this._context.DestPositionAd.Any(x => x.PositionId == positionId);
        }

        public IPosition FindById(int id)
        {
            Check.Argument.IsNotNegativeOrZero(id, "id");
            return this._context.Position.Include("Template").FirstOrDefault(p => p.PositionId == id);
        }

        public IQueryable<IPosition> FindByIds(string positions)
        {
            return this._context.Position.Where("it.PositionId in {" + positions + "}");
        }

        public IQueryable<IPosition> FindAll()
        {
            return this._context.Position.OrderByDescending(p => p.PositionId);
        }

        public PagedResult<IPosition> FindAll(int pageSize, int pageIndex)
        {
            var positions = this._context.Position.OrderByDescending(p => p.PositionId)
                .Skip(pageSize * pageIndex)
                .Take(pageSize);
            var count = CountAll();

            return BuildPagedResult<IPosition>(positions, count);
        }

        public PagedResult<IPosition> FindByUniqueCode(int pageSize, int pageIndex, string uniqueCode)
        {
            if (string.IsNullOrEmpty(uniqueCode))
            {
                return this.FindAll(pageSize, pageIndex);
            }

            var positions = this._context.Position.Where(c => c.UniqueCode.Contains(uniqueCode))
                .OrderByDescending(p => p.CreateDate)
                .Skip(pageSize * pageIndex)
                .Take(pageSize);
            var count = positions.Count();

            return BuildPagedResult<IPosition>(positions, count);
        }

        public int CountAll()
        {
            return this._context.Position.Count();
        }

        public PagedResult<IPosition> FindByName(int pageSize, int pageIndex, string name)
        {
            var positions = this._context.Position.Where(p => p.Name.Contains(name))
                .OrderByDescending(p => p.PositionId)
                .Skip(pageSize * pageIndex)
                .Take(pageSize);
            var count = CountByName(name);

            return BuildPagedResult<IPosition>(positions, count);
        }

        public int CountByName(string name)
        {
            return this._context.Position.Where(p => p.Name.Contains(name)).Count();
        }
    }
}
